package com.zyl.musicdemo.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.zyl.musicdemo.R;
import com.zyl.musicdemo.activitys.MainActivity;
import com.zyl.musicdemo.activitys.WelcomeActivity;
import com.zyl.musicdemo.bean.MusicModel;
import com.zyl.musicdemo.helps.MediaPlayerHelp;

/**
 * 1、通过Service 连接 PlayMusicView 和 MediaPlayHelper
 * 2、PlayMusicView -- Service：
 * 1、播放音乐、暂停音乐
 * 2、启动Service、绑定Service、解除绑定Service
 * 3、MediaPlayHelper -- Service：
 * 1、播放音乐、暂停音乐
 * 2、监听音乐播放完成，停止 Service
 */
public class MusicService extends Service {

    public static final int NOTIFICATION_ID = 1;

    private MediaPlayerHelp mMediaPlayerHelp;
    private MusicModel mMusicModel;

    public class MusicBind extends Binder {

        public void setMusicModel(MusicModel musicModel) {
            mMusicModel = musicModel;
            startForeground();//启动前台
        }

        /**
         * 播放音乐
         */
        public void playMusic() {
            /**
             * 1、判断当前音乐是否在播放
             * 2、如果当前在播放，那么直接start方法
             * 3、如果没有播放当前点击的音乐，那就setPath方法
             */

            if (mMediaPlayerHelp.getPath() != null
                    && mMediaPlayerHelp.getPath().equals(mMusicModel.getPath())) {
                mMediaPlayerHelp.start();
            } else {
                mMediaPlayerHelp.setPath(mMusicModel.getPath());
                mMediaPlayerHelp.setOnMeidaPlayerHelperListener(new MediaPlayerHelp.OnMeidaPlayerHelperListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mMediaPlayerHelp.start();
                    }

                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        stopSelf();
                    }
                });
            }

        }

        /**
         * 停止播放音乐
         */
        public void stopMusic() {
            mMediaPlayerHelp.pause();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MusicBind();
    }


    @Override
    public void onCreate() {
        super.onCreate();
        mMediaPlayerHelp = MediaPlayerHelp.getInstance(this);
    }

    /**
     * * 系统默认不允许不可见的后台服务播放音乐，
     * Notification ，
     * 设置服务在前台可见
     */

    private void startForeground() {
        /**
         * 通知栏点击 进入应用
         */

//        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
//                new Intent(this, WelcomeActivity.class),
//                PendingIntent.FLAG_CANCEL_CURRENT);

        /**
         * 现在没有做登入界面逻辑  所以我们直接跳转到MainActivity  否则跳转到WelcomeActivity检查登入
         */
        PendingIntent pendingIntent = PendingIntent
                .getActivity(this, 0, new Intent(this,
                                MainActivity.class),
                        PendingIntent.FLAG_CANCEL_CURRENT);


        /**
         * 创建Notification
         */
        Notification notification = null;
        /**
         * android API 26 以上 NotificationChannel 特性适配
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = createNotificationChannel();
            notification = new Notification.Builder(this, channel.getId())
                    .setContentTitle(mMusicModel.getName())
                    .setContentText(mMusicModel.getAuthor())
                    .setSmallIcon(R.drawable.ic_baseline_music_note_red_24)
                    .setContentIntent(pendingIntent)
                    .build();

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);

        } else {
            notification = new Notification.Builder(this)
                    .setContentTitle(mMusicModel.getName())
                    .setContentText(mMusicModel.getAuthor())
                    .setSmallIcon(R.drawable.ic_baseline_music_note_red_24)
                    .setContentIntent(pendingIntent)
                    .build();
        }

        //设置显示前台服务
        startForeground(NOTIFICATION_ID, notification);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private NotificationChannel createNotificationChannel() {
        String channelId = "music";
        String channelName = "musicMusicService";
        String Description = "MyMusic";
        NotificationChannel channel = new NotificationChannel(channelId, channelName,
                NotificationManager.IMPORTANCE_HIGH);

        channel.setDescription(Description);
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        channel.setShowBadge(false);

        return channel;
    }

}
